package org.jim.connector.server;

import com.huiyun.project.common.core.util.SpringUtils;
import org.apache.commons.lang3.StringUtils;
import org.jim.connector.config.JimProperties;
import org.jim.connector.listener.ImDemoGroupListener;
import org.jim.connector.listener.ImDemoUserListener;
import org.jim.core.helper.*;
import org.jim.core.helper.cs.ICsChatHelper;
import org.jim.core.helper.cs.ICsSessionHelper;
import org.jim.core.helper.im.IFriendHelper;
import org.jim.core.helper.im.IGroupHelper;
import org.jim.core.utils.PropUtil;
import org.jim.server.JimServer;
import org.jim.server.config.ImServerConfig;
import org.jim.server.config.PropertyImServerConfigBuilder;
import org.jim.server.helper.MessageHelperImpl;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.stereotype.Component;
import org.tio.core.ssl.SslConfig;

import java.util.Arrays;
import java.util.List;

/**
 * @FileName:
 * @Description:
 * @Author linjie
 * @Date 2020/7/144:26 PM
 * @Version 4.0.0
 */
@Component
@EnableConfigurationProperties(JimProperties.class)
public class ImServerStart implements CommandLineRunner {

//    @Autowired
//    private JimProperties jimProperties;

    @Override
    public void run(String... args) throws Exception {
        List profileList = Arrays.asList(SpringUtils.getApplicationContext().getEnvironment().getActiveProfiles());
        String file = "jim.properties";
        if (profileList.contains("sit")) {
            file = "jim-sit.properties";
        } else if (profileList.contains("uat")) {
            file = "jim-uat.properties";
        } else if (profileList.contains("prd")) {
            file = "jim-prd.properties";
        }
        //将类转properties
        ImServerConfig imServerConfig = new PropertyImServerConfigBuilder(file).build();
        //初始化SSL;(开启SSL之前,你要保证你有SSL证书哦...)
        initSsl(imServerConfig);
        //设置群组监听器，非必须，根据需要自己选择性实现;
        imServerConfig.setImGroupListener(new ImDemoGroupListener());
        //设置绑定用户监听器，非必须，根据需要自己选择性实现;
        imServerConfig.setImUserListener(new ImDemoUserListener());
        /**
         * 添加实现,最小代价将messageHelper拆分
         */
        imServerConfig.setMessageHelper(new MessageHelperImpl(
                SpringUtils.getBean(IUserHelper.class),
                SpringUtils.getBean(IGroupHelper.class),
                SpringUtils.getBean(IFriendHelper.class),
                SpringUtils.getBean(ICsChatHelper.class),
                SpringUtils.getBean(ICsSessionHelper.class)
        ));

        JimServer jimServer = new JimServer(imServerConfig);
        jimServer.start();
    }

    /**
     * 开启SSL之前，你要保证你有SSL证书哦！
     *
     * @param imServerConfig
     * @throws Exception
     */
    private static void initSsl(ImServerConfig imServerConfig) throws Exception {
        //开启SSL
        if (ImServerConfig.ON.equals(imServerConfig.getIsSSL())) {
            String keyStorePath = PropUtil.get("jim.key.store.path");
            String keyStoreFile = keyStorePath;
            String trustStoreFile = keyStorePath;
            String keyStorePwd = PropUtil.get("jim.key.store.pwd");
            if (StringUtils.isNotBlank(keyStoreFile) && StringUtils.isNotBlank(trustStoreFile)) {
                SslConfig sslConfig = SslConfig.forServer(keyStoreFile, trustStoreFile, keyStorePwd);
                imServerConfig.setSslConfig(sslConfig);
            }
        }
    }
}
